#include "mythread.h"
#include <QDateTime>
#include <QMutex>

#define LOCKING
//#define PERTHREADING

#ifdef PERTHREADING
__thread int i = 0;
#else
int i = 0;
#endif

#ifdef LOCKING
QMutex iM;
#endif

MyThread::MyThread()
{
    i = 0;
    ulong l = QDateTime::currentDateTime().toTime_t();
    qDebug ("dddddd === %d ",l );
    qsrand(l);
}

void MyThread::run()
{
#ifdef LOCKING
    iM.lock();
#endif
    int ib;
    emit changed (++i);   ib = i;
    if (ib!= i) qDebug("0 Fuck. LOCKING eating my brain!!!!");
#ifdef LOCKING
    iM.unlock();
#endif
    while (true){
        for (long t = 0; t< qAbs(1000000*(50-i)); t++){
            double tt = t*qrand ();
        }
#ifdef LOCKING
        iM.lock();
#endif
        emit changed (++i); ib = i;
        //
        //        qDebug()<< objectName() << " == "<< i;
        msleep(1+ (qrand()%30));
        if (ib!= i) qDebug("2 Fuck. LOCKING eating my brain!!!!");
        emit changed (++i); ib = i;
        if (ib!= i) qDebug("3 Fuck. LOCKING eating my brain!!!!");
#ifdef LOCKING
        iM.unlock();
#endif
    }

}
